<template>
  <div class="mb20">
    <jxf-titletip :title="'明细'" :hasRefresh="false" :hasGoBack="true"
                  @goBack="$emit('update:isShowDetail', false)"></jxf-titletip>
    <el-card class="jxf-table-card mt20">
      <div slot="header">
        <span>数据列表</span>
        <div>
          <el-button class="mr20" v-if="auditInfo.status === 0" type="primary" @click="showReview">审核</el-button>
          <el-button type="primary" @click="exportData">导出</el-button>
        </div>
      </div>
      <jxf-table :table="tableInfo">
        <template slot="rate" slot-scope="scope">
          <span>{{scope.row.rate !== "" && scope.row.rate !== null ? parseInt(scope.row.rate * 10000)/100  + "%" : ""}}</span>
        </template>
      </jxf-table>
    </el-card>
  </div>
</template>

<script>
  import formatDate from '@/assets/js/formatDate'

  export default {
    name: "BusinessTransferAuditDetail",
    props: {
      isShowDetail: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        auditInfo: {
          status: null
        },
        tableInfo: {}
      }
    },
    methods: {
      // 列表初始化
      init() {
        this.tableInfo = {
          headerStyle: {background: '#f9f9f9'},
          border: true,
          loading: true,
          hasShowSummary: true,
          tr: [
            {
              id: '1',
              minWidth: '120',
              label: '转出单号（ID）',
              prop: 'applyNo',
            },
            {
              id: '2',
              minWidth: '120',
              label: '订单编号',
              prop: 'orderNo'
            },
            {
              id: '3',
              width: '160',
              label: '订单完成时间',
              prop: 'payTime'
            },
            {
              id: '4',
              width: '160',
              label: '申请转出时间',
              prop: 'applyTime'
            },
            {
              id: '5',
              minWidth: '120',
              label: '订单结算金额',
              prop: 'amount'
            },
            {
              id: '6',
              label: '转出金额',
              prop: 'actualAmount'
            },
            {
              id: '7',
              label: '提前月份',
              prop: 'count'
            },
            {
              id: '8',
              label: '计算费率',
              show: "template",
              prop: 'rate'
            },
            {
              id: '9',
              minWidth: '120',
              label: '提前转出保理费',
              prop: 'facAmount'
            },
            {
              id: '10',
              width: '160',
              label: '结算时间',
              prop: 'settleTime'
            }],
          data: [],
          getSummaries(param) {
            const {columns, data} = param;
            const sums = [];
            columns.forEach((column, index) => {
              if (index === 0) {
                sums[index] = '合计';
                return;
              }
              let values = []
              if (["amount", "facAmount", "actualAmount"].indexOf(column.property) >= 0) {
                values = data.map(item => Number(item[column.property]));
              }
              if (!values.every(value => isNaN(value))) {
                sums[index] = values.reduce((prev, curr) => {
                  const value = Number(curr);
                  if (!isNaN(value)) {
                    return (parseInt(prev * 100) + parseInt(curr * 100))/100;
                  } else {
                    return prev;
                  }
                }, 0);
                sums[index] = parseInt(sums[index]*100)/100 + ' 元';
              } else {
                sums[index] = '';
              }

            });

            return sums;
          }
        }
      },
      getApplyOrderList(row) {
        this.auditInfo = {
          id: row.id,
          status: row.status,
          enterpriseName: row.enterpriseName,
          amount: row.amount,
          facAmount: row.facAmount,
          applyTime: row.applyTime
        }
        ajax.get(apis.factory.getApplyOrderList, {applyId: row.id})
          .then(res => {
            if (res.success === true) {
              if (res.data !== [] && res.data !== null) {
                this.tableInfo.data = this.formatshowData(res.data)
              } else {
                this.tableInfo.data = []
              }
              this.tableInfo.loading = false
            }
          })
      },
      // 数据处理
      formatshowData(info) {
        return info.map(item => {
          return {
            applyNo: item.applyNo,
            orderNo: item.orderNo,
            payTime: item.payTime !== null && item.payTime !== "" ? formatDate.timeFormat(item.payTime) : "",
            applyTime: item.applyTime !== null && item.applyTime !== "" ? formatDate.timeFormat(item.applyTime) : "",
            amount: item.amount,
            count: item.count,
            rate: item.rate,
            facAmount: item.facAmount,
            actualAmount: item.actualAmount,
            settleTime: item.settleTime !== null && item.settleTime !== "" ? formatDate.timeFormat(item.settleTime) : ""
          }
        })
      },
      showReview() {
        this.$emit('showReview', this.auditInfo)
      },
      exportData(){
        ajax.export(apis.factory.exportApplyOrderList, {applyId:this.auditInfo.id})
          .then(res => {})
      }
    },
    created() {
      this.init()
    }
  }
</script>

<style lang="scss" scoped>

</style>
